﻿
    using System;
    using System.Collections.Generic;
    using System.Runtime.CompilerServices;
    using System.Windows;
    using System.Windows.Controls;
namespace WCYCReader.Drawing
{
    internal class GraphicsRecorder : GraphicsExtended, Graphics
    {
        private List<PlayAction> actions;
        private GraphicsExtendedImpl graphics;

        internal GraphicsRecorder(int width, int height)
        {
            this.graphics = new GraphicsExtendedImpl(width, height);
            this.actions = new List<PlayAction>();
            this.setColor(-1);
            this.setTransform(new AffineTransformImpl());
        }

        public bool canRenderGlyphs()
        {
            return this.graphics.canRenderGlyphs();
        }

        public void clearRect(int x, int y, int width, int height)
        {
            PlayAction action2 = null;
            PlayAction item = null;
            if ((width > 0) && (height > 0))
            {
                lock (this.actions)
                {
                    if (((x == 0) && (y == 0)) && ((width >= this.graphics.Width) && (height >= this.graphics.Height)))
                    {
                        this.actions.Clear();
                        this.setARGBColor(this.graphics.getARGBColor());
                        this.setBackground(this.graphics.BackgroundColor);
                        this.setTransform(this.graphics.RenderTransform);
                    }
                    if (item == null)
                    {
                        if (action2 == null)
                        {
                            action2 = g => g.clearRect(x, y, width, height);
                        }
                        item = action2;
                    }
                    this.actions.Add(item);
                }
            }
        }

        public void colorBlit(Rectangle rectangle, int color, int operation)
        {
            PlayAction action2 = null;
            PlayAction item = null;
            lock (this.actions)
            {
                if (item == null)
                {
                    if (action2 == null)
                    {
                        action2 = g => g.colorBlit(rectangle, color, operation);
                    }
                    item = action2;
                }
                this.actions.Add(item);
            }
        }

        public void drawArc(int x, int y, int width, int height, int startAngle, int arcAngle)
        {
            PlayAction action2 = null;
            PlayAction item = null;
            lock (this.actions)
            {
                if (item == null)
                {
                    if (action2 == null)
                    {
                        action2 = g => g.drawArc(x, y, width, height, startAngle, arcAngle);
                    }
                    item = action2;
                }
                this.actions.Add(item);
            }
        }

        public void drawBufferedImage(BufferedImage bufferedImage, int x, int y)
        {
            PlayAction action2 = null;
            PlayAction item = null;
            lock (this.actions)
            {
                if (item == null)
                {
                    if (action2 == null)
                    {
                        action2 = g => g.drawBufferedImage(bufferedImage, x, y);
                    }
                    item = action2;
                }
                this.actions.Add(item);
            }
        }

        public void drawImage(BufferedImageExtended bufferedImage, AffineTransform transform)
        {
            PlayAction action2 = null;
            PlayAction item = null;
            lock (this.actions)
            {
                if (item == null)
                {
                    if (action2 == null)
                    {
                        action2 = g => g.drawImage(bufferedImage, transform);
                    }
                    item = action2;
                }
                this.actions.Add(item);
            }
        }

        public void drawImage(Image image, int x, int y)
        {
            PlayAction action2 = null;
            PlayAction item = null;
            lock (this.actions)
            {
                if (item == null)
                {
                    if (action2 == null)
                    {
                        action2 = g => g.drawImage(image, x, y);
                    }
                    item = action2;
                }
                this.actions.Add(item);
            }
        }

        public void drawImage(BufferedImageExtended bufferedImage, int x, int y, int width, int height, int left, int top)
        {
            PlayAction action2 = null;
            PlayAction item = null;
            lock (this.actions)
            {
                if (item == null)
                {
                    if (action2 == null)
                    {
                        action2 = g => g.drawImage(bufferedImage, x, y, width, height, left, top);
                    }
                    item = action2;
                }
                this.actions.Add(item);
            }
        }

        public void drawLine(int x1, int y1, int x2, int y2)
        {
            PlayAction action2 = null;
            PlayAction item = null;
            lock (this.actions)
            {
                if (item == null)
                {
                    if (action2 == null)
                    {
                        action2 = g => g.drawLine(x1, y1, x2, y2);
                    }
                    item = action2;
                }
                this.actions.Add(item);
            }
        }

        public void drawRect(int x, int y, int width, int height)
        {
            PlayAction action2 = null;
            PlayAction item = null;
            lock (this.actions)
            {
                if (item == null)
                {
                    if (action2 == null)
                    {
                        action2 = g => g.drawRect(x, y, width, height);
                    }
                    item = action2;
                }
                this.actions.Add(item);
            }
        }

        public void drawText(char[] text, int offset, int length, int x, int y, int width)
        {
            PlayAction action2 = null;
            PlayAction item = null;
            lock (this.actions)
            {
                if (item == null)
                {
                    if (action2 == null)
                    {
                        action2 = g => g.drawText(text, offset, length, x, y, width);
                    }
                    item = action2;
                }
                this.actions.Add(item);
            }
        }

        public void fill(GeneralPath path)
        {
            PlayAction action2 = null;
            PlayAction item = null;
            lock (this.actions)
            {
                if (item == null)
                {
                    if (action2 == null)
                    {
                        action2 = g => g.fill(path);
                    }
                    item = action2;
                }
                this.actions.Add(item);
            }
        }

        public void fill(Polygon r)
        {
            PlayAction action2 = null;
            PlayAction item = null;
            lock (this.actions)
            {
                if (item == null)
                {
                    if (action2 == null)
                    {
                        action2 = g => g.fill(r);
                    }
                    item = action2;
                }
                this.actions.Add(item);
            }
        }

        public void fill(Rectangle r)
        {
            PlayAction action2 = null;
            PlayAction item = null;
            lock (this.actions)
            {
                if (item == null)
                {
                    if (action2 == null)
                    {
                        action2 = g => g.fill(r);
                    }
                    item = action2;
                }
                this.actions.Add(item);
            }
        }

        public void fillArc(int x, int y, int width, int height, int startAngle, int arcAngle)
        {
            PlayAction action2 = null;
            PlayAction item = null;
            lock (this.actions)
            {
                if (item == null)
                {
                    if (action2 == null)
                    {
                        action2 = g => g.fillArc(x, y, width, height, startAngle, arcAngle);
                    }
                    item = action2;
                }
                this.actions.Add(item);
            }
        }

        public void fillRect(int x, int y, int width, int height)
        {
            PlayAction action2 = null;
            PlayAction item = null;
            lock (this.actions)
            {
                if (item == null)
                {
                    if (action2 == null)
                    {
                        action2 = g => g.fillRect(x, y, width, height);
                    }
                    item = action2;
                }
                this.actions.Add(item);
            }
        }

        public int getARGBColor()
        {
            return this.graphics.getARGBColor();
        }

        public int getColor()
        {
            return this.graphics.getColor();
        }

        public Font getFont()
        {
            return this.graphics.getFont();
        }

        public AffineTransform getTransform()
        {
            return this.graphics.getTransform();
        }

        public void invert(int x, int y, int width, int height)
        {
            PlayAction action2 = null;
            PlayAction item = null;
            lock (this.actions)
            {
                if (item == null)
                {
                    if (action2 == null)
                    {
                        action2 = g => g.invert(x, y, width, height);
                    }
                    item = action2;
                }
                this.actions.Add(item);
            }
        }

        public bool needsRendering()
        {
            return this.graphics.needsRendering();
        }

        internal Canvas Playback(GraphicsExtendedImpl g, Rect clip)
        {
            if (g == null)
            {
                g = new GraphicsExtendedImpl(this.graphics.Width, this.graphics.Height);
            }
            Canvas frameBuffer = g.FrameBuffer;
            Rect playbackClip = g.PlaybackClip;
            g.PlaybackClip = clip;
            lock (this.actions)
            {
                foreach (PlayAction action in this.actions)
                {
                    action(g);
                }
            }
            g.PlaybackClip = playbackClip;
            return frameBuffer;
        }

        public void scale(double x, double y)
        {
            PlayAction action2 = null;
            PlayAction item = null;
            lock (this.actions)
            {
                if (item == null)
                {
                    if (action2 == null)
                    {
                        action2 = g => g.scale(x, y);
                    }
                    item = action2;
                }
                this.actions.Add(item);
            }
            this.graphics.scale(x, y);
        }

        public void setAntialias()
        {
            lock (this.actions)
            {
                this.actions.Add(g => g.setAntialias());
            }
        }

        public void setARGBColor(int color)
        {
            PlayAction action2 = null;
            PlayAction item = null;
            lock (this.actions)
            {
                if (item == null)
                {
                    if (action2 == null)
                    {
                        action2 = g => g.setARGBColor(color);
                    }
                    item = action2;
                }
                this.actions.Add(item);
            }
            this.graphics.setARGBColor(color);
        }

        public void setBackground(int RGB)
        {
            PlayAction action2 = null;
            PlayAction item = null;
            lock (this.actions)
            {
                if (item == null)
                {
                    if (action2 == null)
                    {
                        action2 = g => g.setBackground(RGB);
                    }
                    item = action2;
                }
                this.actions.Add(item);
            }
            this.graphics.setBackground(RGB);
        }

        public void setColor(int rgb)
        {
            PlayAction action2 = null;
            PlayAction item = null;
            lock (this.actions)
            {
                if (item == null)
                {
                    if (action2 == null)
                    {
                        action2 = g => g.setColor(rgb);
                    }
                    item = action2;
                }
                this.actions.Add(item);
            }
            this.graphics.setColor(rgb);
        }

        public void setFont(Font font)
        {
            PlayAction action2 = null;
            PlayAction item = null;
            lock (this.actions)
            {
                if (item == null)
                {
                    if (action2 == null)
                    {
                        action2 = g => g.setFont(font);
                    }
                    item = action2;
                }
                this.actions.Add(item);
            }
            this.graphics.setFont(font);
        }

        public void setTransform(AffineTransform aT)
        {
            PlayAction action2 = null;
            PlayAction item = null;
            lock (this.actions)
            {
                if (item == null)
                {
                    if (action2 == null)
                    {
                        action2 = g => g.RenderTransform = (AffineTransformImpl) aT;
                    }
                    item = action2;
                }
                this.actions.Add(item);
                this.graphics.setTransform(aT);
            }
        }

        public void translate(double x, double y)
        {
            PlayAction action2 = null;
            PlayAction item = null;
            lock (this.actions)
            {
                if (item == null)
                {
                    if (action2 == null)
                    {
                        action2 = g => g.translate(x, y);
                    }
                    item = action2;
                }
                this.actions.Add(item);
            }
            this.graphics.translate(x, y);
        }

        public void translate(int x, int y)
        {
            PlayAction action2 = null;
            PlayAction item = null;
            lock (this.actions)
            {
                if (item == null)
                {
                    if (action2 == null)
                    {
                        action2 = g => g.translate(x, y);
                    }
                    item = action2;
                }
                this.actions.Add(item);
            }
            this.graphics.translate(x, y);
        }

        private delegate void PlayAction(GraphicsExtendedImpl g);
    }
}

